package com.sg.controller.bff;

import com.sg.service.biz.bff.CommonService;
import com.sg.dto.biz.bff.req.*;
import com.sg.dto.biz.bff.res.*;
import com.wicket.okrbff.common.annotations.BanAuto;
import com.sg.common.ro.OkrResult;
import com.wicket.okrbff.common.tools.StringUtils;
import com.sg.common.util.CommonFunctionHelper;
import com.wicket.okrcomponent.integration.UnstructuredDataService;
import com.sg.dto.integration.component.AddDocumentComReqDto;
import com.sg.dto.integration.component.AddDocumentComRespDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;

/**
 * @author ：
 * @version 1.0
 * @date ：
 */
@Api(value = "Common", tags = {"Common"})
@RestController
// @RequestMapping("/oms/common/")
public class CommonController {

    @Autowired
    CommonService commonService;

    @Autowired
    UnstructuredDataService unstructuredDataService;

    /**
     * B查询前端公共信息列表（公共）
     * gen by moon at 9/2/2022, 3:00:09 PM
     */
    @PostMapping("/oms/common/queryFrontPublicInfoList")
    @ApiOperation(value = "B查询前端公共信息列表（公共）", response = OkrResult.class)
    public OkrResult<BffQueryFrontPublicInfoListRespDto> queryFrontPublicInfoList(@RequestBody(required = false) @Validated BffQueryFrontPublicInfoListReqDto reqDTO) {
        return OkrResult.success(commonService.queryFrontPublicInfoList(reqDTO));
    }

    /**
     * B查询空间框架信息前端缓存（公共）
     * gen by moon at 8/10/2022, 3:31:54 AM
     */
    @PostMapping("/oms/common/queryFrameInfoFrontCacheDetail")
    @ApiOperation(value = "B查询空间框架信息前端缓存（公共）", response = OkrResult.class)
    public OkrResult<BffQueryFrameInfoFrontCacheDetailRespDto> queryFrameInfoFrontCacheDetail(@RequestBody(required = false) @Validated BffQueryFrameInfoFrontCacheDetailReqDto reqDTO) {
        return OkrResult.success(commonService.queryFrameInfoFrontCacheDetail(reqDTO));
    }

    @PostMapping("/oms/common/addUploadFile")
    @ApiOperation(value = "图片上传", response = OkrResult.class)
    @BanAuto
    public OkrResult<BffUpLoadImageRespDto> uploadImage(@RequestBody(required = false) BffUpLoadImageReqDto reqDto) throws Exception {
        //final String path = commonService.uploadImage(reqDto.getScope(), reqDto.getUse(), reqDto.getSpaceId(), reqDto.getFile());
        BffUpLoadImageRespDto retData = new BffUpLoadImageRespDto();
//        retData.setUrl(CommonFunctionHelper.getUrlPath(path));
        return OkrResult.success(retData);
    }


    @PostMapping("/oms/common/addUploadNormalFile")
    @ApiOperation(value = "图片上传", response = OkrResult.class)
    @BanAuto
    public OkrResult<BffUpLoadImageRespDto> uploadNoramlImage(@RequestParam(value = "scope", required = false) String scope,
                                                              @RequestParam("use") String use,
                                                              @RequestParam(value = "spaceId", required = false) String spaceId,
                                                              MultipartFile file) throws Exception {
        BffUpLoadImageRespDto retData = new BffUpLoadImageRespDto();
        try {
            //final String path = commonService.uploadNoramlImage(scope, use, spaceId, file);
            String path = null;

            retData.setUrl(CommonFunctionHelper.getFilePath(path));
            retData.setRelaUrl("/" + path);
            retData.setFileLength(file.getSize());
            retData.setFileName(file.getOriginalFilename());
            if (!StringUtils.isEmpty(retData.getFileName()) && retData.getFileName().lastIndexOf(".") >= 0) {
                String type = retData.getFileName().substring(retData.getFileName().lastIndexOf(".") + 1);
                retData.setFileSuffix(type);
            }
            String[] parts = retData.getFileName().split("\\.");
            if (parts != null && parts.length > 0) {
                String nameWithoutExtension = String.join(".", Arrays.copyOf(parts, parts.length - 1));
                retData.setFileName(nameWithoutExtension);
            }

            AddDocumentComReqDto addDocumentComReqDto = new AddDocumentComReqDto();
            addDocumentComReqDto.setFileRelativePathAccess("/" + path);
            addDocumentComReqDto.setPostfix(retData.getFileSuffix());
            addDocumentComReqDto.setFileSize(String.valueOf(retData.getFileLength()));
            addDocumentComReqDto.setUnstructuredDataName(retData.getFileName());
            AddDocumentComRespDto docRetData = unstructuredDataService.addDocumentCom(addDocumentComReqDto).getData();
            retData.setId(docRetData.getUnstructuredDataId());
        } catch (Exception e) {
            System.out.println("上传失败，请重新上传！:" + e);
            return OkrResult.fail("888", "上传失败，请重新上传！");
        }

        return OkrResult.success(retData);
    }

    /**
     * B存档批次下单条数据（公共）
     * gen by moon at 8/10/2022, 3:35:20 AM
     */
    @PostMapping("/oms/common/archiveBatchSingleDataCom")
    @ApiOperation(value = "B存档批次下单条数据（公共）", response = OkrResult.class)
    public OkrResult<BffArchiveBatchSingleDataComRespDto> archiveBatchSingleDataCom(@RequestBody(required = false) @Validated BffArchiveBatchSingleDataComReqDto reqDTO) {
        return OkrResult.success(commonService.archiveBatchSingleDataCom(reqDTO));
    }

    /**
     * B修改排序（公共）
     * gen by moon at 8/10/2022, 3:33:56 AM
     */
    @PostMapping("/oms/common/updateSortNumCom")
    @ApiOperation(value = "B修改排序（公共）", response = OkrResult.class)
    public OkrResult<BffUpdateSortNumComRespDto> updateSortNumCom(@RequestBody(required = false) @Validated BffUpdateSortNumComReqDto reqDTO) {
        return OkrResult.success(commonService.updateSortNumCom(reqDTO));
    }

    /**
     * B发布单条数据（公共）
     * gen by moon at 8/10/2022, 3:29:38 AM
     */
    @PostMapping("/oms/common/releaseSingleDataCom")
    @ApiOperation(value = "B发布单条数据（公共）", response = OkrResult.class)
    public OkrResult<BffReleaseSingleDataComRespDto> releaseSingleDataCom(@RequestBody(required = false) @Validated BffReleaseSingleDataComReqDto reqDTO) {
        return OkrResult.success(commonService.releaseSingleDataCom(reqDTO));
    }

    /**
     * B撤销存档批次下单条数据（公共）
     * gen by moon at 8/10/2022, 3:29:34 AM
     */
    @PostMapping("/oms/common/undoArchiveBatchSingleDataCom")
    @ApiOperation(value = "B撤销存档批次下单条数据（公共）", response = OkrResult.class)
    public OkrResult<BffUndoArchiveBatchSingleDataComRespDto> undoArchiveBatchSingleDataCom(@RequestBody(required = false) @Validated BffUndoArchiveBatchSingleDataComReqDto reqDTO) {
        return OkrResult.success(commonService.undoArchiveBatchSingleDataCom(reqDTO));
    }

    /**
     * B批量修改排序（公共）
     * gen by moon at 8/10/2022, 3:37:53 AM
     */
    @PostMapping("/oms/common/batchUpdateSortNum")
    @ApiOperation(value = "B批量修改排序（公共）", response = OkrResult.class)
    public OkrResult<BffBatchUpdateSortNumRespDto> batchUpdateSortNum(@RequestBody(required = false) @Validated BffBatchUpdateSortNumReqDto reqDTO) {
        return OkrResult.success(commonService.batchUpdateSortNum(reqDTO));
    }

    /**
     * B删除单条数据（公共）
     * gen by moon at 8/10/2022, 3:35:18 AM
     */
    @PostMapping("/oms/common/deleteSingleDataCom")
    @ApiOperation(value = "B删除单条数据（公共）", response = OkrResult.class)
    public OkrResult<BffDeleteSingleDataComRespDto> deleteSingleDataCom(@RequestBody(required = false) @Validated BffDeleteSingleDataComReqDto reqDTO) {
        return OkrResult.success(commonService.deleteSingleDataCom(reqDTO));
    }

    /**
     * B存档单条数据（公共）
     * gen by moon at 8/10/2022, 3:29:11 AM
     */
    @PostMapping("/oms/common/archiveSingleDataCom")
    @ApiOperation(value = "B存档单条数据（公共）", response = OkrResult.class)
    public OkrResult<BffArchiveSingleDataComRespDto> archiveSingleDataCom(@RequestBody(required = false) @Validated BffArchiveSingleDataComReqDto reqDTO) {
        return OkrResult.success(commonService.archiveSingleDataCom(reqDTO));
    }

    /**
     * B查询城市列表
     * gen by moon at 8/10/2022, 3:33:04 AM
     */
    @PostMapping("/oms/common/queryQueryCityListList")
    @ApiOperation(value = "B查询城市列表", response = OkrResult.class)
    public OkrResult<BffQueryQueryCityListListRespDto> queryQueryCityListList(@RequestBody(required = false) @Validated BffQueryQueryCityListListReqDto reqDTO) {
        return OkrResult.success(commonService.queryQueryCityListList(reqDTO));
    }

    /**
     * B1-2上传组织架构模板
     * gen by moon at 8/10/2022, 3:31:47 AM
     */
    @PostMapping("/oms/mgt/common/addUploadOrgTemplate")
    @ApiOperation(value = "B1-2上传组织架构模板", response = OkrResult.class)
    public OkrResult<BffAddUploadOrgTemplateRespDto> addUploadOrgTemplate(@RequestBody(required = false) @Validated BffAddUploadOrgTemplateReqDto reqDTO) {
        return OkrResult.success(commonService.addUploadOrgTemplate(reqDTO));
    }


}
